/**
 * @file    drv_adc.c
 * @brief   内部ADC驱动
 * @author  name
 * @version 1.0.1
 * @date    2023-04-14
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-14 <td>1.0.1     <td>LW       <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "drv_adc.h"

/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  adc驱动初始化              
 * @param  void
 * @return void            
 */
void drv_adc_init(void)
{
    /* Enable EADC module clock */
    CLK_EnableModuleClock(EADC_MODULE);

     /* EADC clock source is HIRC, set divider to 12, ADC clock is 1 MHz */
    CLK_SetModuleClock(EADC_MODULE, 0, CLK_CLKDIV0_EADC(12));

    /* Configure the GPB0 - GPB3 ADC analog input pins.  */
    SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB3MFP_Msk | 
					   SYS_GPB_MFPL_PB4MFP_Msk |
					   SYS_GPB_MFPL_PB6MFP_Msk |
					   SYS_GPB_MFPL_PB7MFP_Msk );
    SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB3MFP_EADC0_CH3
					  | SYS_GPB_MFPL_PB4MFP_EADC0_CH4
	                  | SYS_GPB_MFPL_PB6MFP_EADC0_CH6 
	                  | SYS_GPB_MFPL_PB7MFP_EADC0_CH7);

    SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB10MFP_Msk| SYS_GPB_MFPH_PB11MFP_Msk|
                       SYS_GPB_MFPH_PB14MFP_Msk);
    SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB10MFP_EADC0_CH10 | SYS_GPB_MFPH_PB11MFP_EADC0_CH11 |
                      SYS_GPB_MFPH_PB14MFP_EADC0_CH14 );

    /* Disable the GPB6 - GPB15 digital input path to avoid the leakage current. */
    GPIO_DISABLE_DIGITAL_PATH(PB, BIT3|BIT4|BIT6|BIT7|BIT10| 
                                  BIT11|BIT14);
							  
    EADC_Open(EADC, (uint32_t)NULL);

    EADC_ConfigSampleModule(EADC, 0, EADC_SOFTWARE_TRIGGER, 3);
    EADC_ConfigSampleModule(EADC, 1, EADC_SOFTWARE_TRIGGER, 4);
    EADC_ConfigSampleModule(EADC, 2, EADC_SOFTWARE_TRIGGER, 6);
    EADC_ConfigSampleModule(EADC, 3, EADC_SOFTWARE_TRIGGER, 7);
    EADC_ConfigSampleModule(EADC, 4, EADC_SOFTWARE_TRIGGER, 10);
    EADC_ConfigSampleModule(EADC, 5, EADC_SOFTWARE_TRIGGER, 11);
    EADC_ConfigSampleModule(EADC, 6, EADC_SOFTWARE_TRIGGER, 14);
	
	drv_adc_start();
}

/**
 * @brief  启动ADC转换
 * @param  void
 * @return void
 */
void drv_adc_start(void)
{
    EADC_START_CONV(EADC,  BIT0  | BIT1 |
	                       BIT2 | BIT3 | BIT4 | BIT5 | BIT6);
}

/**
 * @brief  停止ADC转换
 * @param  void
 * @return void
 */
void drv_adc_stop(void)
{
    EADC_STOP_CONV(EADC, BIT0  | BIT1 |
	                       BIT2 | BIT3 | BIT4 | BIT5 | BIT6);
}

void drv_adc_deinit(void)
{
	drv_adc_stop();
	
	EADC_Close(EADC);
}


/**
 * @brief  读取ADC某个通道电压
 * @param  chl_id : ADC通道
 * @return ADC采样电压值 mV
 */
uint16_t drv_adc_read_chal(uint8_t chl_id)
{ 
    uint16_t read_val = 0;

    if(chl_id >= EADC_CHNL_MAX)
        return 0;

    read_val = EADC_GET_CONV_DATA(EADC, chl_id);
	
    read_val = ADC_VREF_VOLT * read_val / ADC_VREF_REG ; 
	
    return read_val;
}

/**
 * @brief  读取ADC某个通寄存器值
 * @param  chl_id : ADC通道
 * @return ADC采样电压值 mV
 */
uint16_t drv_adc_read_reg(uint8_t chl_id)
{ 
    uint16_t read_val = 0;

    if(chl_id >= EADC_CHNL_MAX)
        return 0;

    read_val = EADC_GET_CONV_DATA(EADC, chl_id);
	
    return read_val;
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/

